﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
{
    //用餐评论表
    public partial class U_Comment
    {

        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from U_Comment");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.UComment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into U_Comment(");
            strSql.Append("Column_10,HotelId,UserId,Comments,CommentTime,jiage,weidao,jchj,Column_8,Column_9");
            strSql.Append(") values (");
            strSql.Append("@Column_10,@HotelId,@UserId,@Comments,@CommentTime,@jiage,@weidao,@jchj,@Column_8,@Column_9");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@Column_10", SqlDbType.Char,10) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Comments", SqlDbType.Text) ,            
                        new SqlParameter("@CommentTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@jiage", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@weidao", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@jchj", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@Column_8", SqlDbType.Char,10) ,            
                        new SqlParameter("@Column_9", SqlDbType.Char,10)             
              
            };

            parameters[0].Value = model.Column_10;
            parameters[1].Value = model.HotelId;
            parameters[2].Value = model.UserId;
            parameters[3].Value = model.Comments;
            parameters[4].Value = model.CommentTime;
            parameters[5].Value = model.jiage;
            parameters[6].Value = model.weidao;
            parameters[7].Value = model.jchj;
            parameters[8].Value = model.Column_8;
            parameters[9].Value = model.Column_9;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.U_Comment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update U_Comment set ");

            strSql.Append(" Column_10 = @Column_10 , ");
            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" UserId = @UserId , ");
            strSql.Append(" Comments = @Comments , ");
            strSql.Append(" CommentTime = @CommentTime , ");
            strSql.Append(" jiage = @jiage , ");
            strSql.Append(" weidao = @weidao , ");
            strSql.Append(" jchj = @jchj , ");
            strSql.Append(" Column_8 = @Column_8 , ");
            strSql.Append(" Column_9 = @Column_9  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Column_10", SqlDbType.Char,10) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Comments", SqlDbType.Text) ,            
                        new SqlParameter("@CommentTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@jiage", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@weidao", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@jchj", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@Column_8", SqlDbType.Char,10) ,            
                        new SqlParameter("@Column_9", SqlDbType.Char,10)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.Column_10;
            parameters[2].Value = model.HotelId;
            parameters[3].Value = model.UserId;
            parameters[4].Value = model.Comments;
            parameters[5].Value = model.CommentTime;
            parameters[6].Value = model.jiage;
            parameters[7].Value = model.weidao;
            parameters[8].Value = model.jchj;
            parameters[9].Value = model.Column_8;
            parameters[10].Value = model.Column_9;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from U_Comment ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from U_Comment ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.UComment GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, Column_10, HotelId, UserId, Comments, CommentTime, jiage, weidao, jchj, Column_8, Column_9  ");
            strSql.Append("  from U_Comment ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            RBS.Core.Model.UComment model = new RBS.Core.Model.UComment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Column_10 = ds.Tables[0].Rows[0]["Column_10"].ToString();
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                model.Comments = ds.Tables[0].Rows[0]["Comments"].ToString();
                if (ds.Tables[0].Rows[0]["CommentTime"].ToString() != "")
                {
                    model.CommentTime = DateTime.Parse(ds.Tables[0].Rows[0]["CommentTime"].ToString());
                }
                model.jiage = ds.Tables[0].Rows[0]["jiage"].ToString();
                model.weidao = ds.Tables[0].Rows[0]["weidao"].ToString();
                model.jchj = ds.Tables[0].Rows[0]["jchj"].ToString();
                model.Column_8 = ds.Tables[0].Rows[0]["Column_8"].ToString();
                model.Column_9 = ds.Tables[0].Rows[0]["Column_9"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM U_Comment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM U_Comment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select u.U_Uid, c.*, CONVERT(varchar(100), c.CommentTime, 20) as distime  FROM U_Comment c left join u_user u on c.UserId=u.Id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }


        
        #endregion  ExtensionMethod

        #region 获取评论数

        /// <summary>
        /// 获取酒店评论数
        /// </summary>
        /// <param name="hotelId"></param>
        /// <returns></returns>
        public int GetHotelCommentCount(int hotelId)
        {
            string strSql = " select COUNT(1) as CommentCount from U_Comment where HotelId=" + hotelId;
             return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }

        public int GetHotelRenjun(int hotelId)
        {
            string strSql = " select SUM(jiage)/COUNT(1) as renjun from U_Comment c where HotelId=" + hotelId;
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        } 
        #endregion

    }
}

